----
title: "Desert Island Behavioral Analyses"
output: word_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r load libraries echo=FALSE}
library(readr)
library(reshape2)
library(plyr)
library(ggplot2)
library(mediation)
library(matrixStats)
library(tidyverse)

```

```{r import echo=FALSE}

#Load behavioral data from matlab

#import data
full_behave_data <- read.csv("~/Dropbox/Mac (3)/Desktop/Studies/DesertIsland/analyses/final_behavioral_analyses/full_behave_data.csv")

#remove trials that don't fall into the rational or irrational condition (for example, where the wrong item was asked about or the irrational decision was made by the contestant)
clean_data = full_behave_data[which(full_behave_data$rational_irrational != 3),]

#Factor and re-label key variables
clean_data$sub <- factor(clean_data$sub)

clean_data$had_did <- factor(clean_data$had_did, levels = c(1,2), labels = c("Force", "Actual"))

clean_data$rational_irrational <- factor(clean_data$rational_irrational, levels = c(1,2), labels = c("Rational", "Irrational"))

#compile mean responses by condition
mean_resps <- aggregate(resp_rev ~ sub + had_did + rational_irrational, data = clean_data, mean)

#create a dataset of difference scores
diff_data <- spread(mean_resps, rational_irrational, resp_rev)
diff_data$diffs <- diff_data$Irrational - diff_data$Rational

#separate out had data from cleandata and difference scores
haddata <- clean_data[which(clean_data$had_did == "Force"),]
haddifs <- diff_data[which(diff_data$had_did == "Force"),]

haddata$sub <- factor(haddata$sub)
haddata$rational_irrational <- factor(haddata$rational_irrational)

#separate out did data from cleandata and difference scores
diddata <- clean_data[which(clean_data$had_did == "Actual"),]
diddifs <- diff_data[which(diff_data$had_did == "Actual"),]

diddata$sub <- factor(diddata$sub)
diddata$rational_irrational <- factor(diddata$rational_irrational)

```
#1. Plot four conditions in bar chart
```{r main bar chart}

cond_legend_title<-"Available Alternative"
fig1_v2<-clean_data %>%
  ggplot(aes(had_did, resp_rev, fill=rational_irrational)) +
  geom_bar(stat="summary", fun.y='mean', position='dodge') +
  geom_errorbar(stat='summary', fun = 'mean', width=.2,
                 position=position_dodge(.9)) +
  geom_point(data = mean_resps, aes(x = had_did, y = resp_rev, color=rational_irrational), stat = "identity", size=2, position=position_jitterdodge(dodge.width=.9,jitter.width = .25), alpha = .75) +
             scale_color_manual(cond_legend_title, values = c("Rational" = "#8a433e", "Irrational" = "#017073")) +
  scale_fill_manual(cond_legend_title, values = c("Rational" = "#F8766D", "Irrational" = "#00BFC4")) +
  xlab("Trial Type") +
  ylab(str_wrap("How strongly do you agree with the statement?", 75)) +
  coord_cartesian(ylim = c(1, 5)) +
  scale_y_continuous(breaks=c(1,2,3,4,5), labels = str_wrap(c("Strongly Disagree","2","3","4","Strongly Agree"),10)) + 
  scale_x_discrete(labels = c("Force", "Actual")) +
  theme_bw() +
  theme(strip.text = element_text(size=14),
        axis.text.x = element_text(size=14),
        axis.text.y = element_text(size=14),
        axis.title = element_text(size=18, vjust=0.35),
        panel.grid = element_blank(),
        legend.position = "right",
        legend.text=element_text(size=14),
        legend.title=element_text(size=16))

ggsave("fig1_v2.jpg",height = 8, width= 12, dpi=1000)

```

#2. Bar graph by difference scores with error bars
```{r secondary bar chart}
#create a version of the data that is ordered by difference score for future plotting

#add subjects difference score to each subject
temp = subset(haddifs, select = c("sub", "diffs"))
merged_dataset <- left_join(haddata, temp, by = 'sub')

#order the data by difference score and set it as an ordered factor
df_ordered <- merged_dataset[order(-merged_dataset$diffs, merged_dataset$sub), ]
df_ordered$sub <- factor(df_ordered$sub)

cond2_legend_title<-"Available Alternative"
fig2b <- df_ordered %>%
  ggplot(aes(x = fct_inorder(sub), resp_rev, fill=rational_irrational)) +
  geom_bar(stat="summary", width = .6, position = position_dodge(.7)) +
  geom_errorbar(stat='summary', width=.2,
                 position=position_dodge(.7)) +
  scale_fill_manual(cond2_legend_title, values = c("Rational" = "#F8766D", "Irrational" = "#00BFC4"), labels = c("Rational", "Irrational")) +
  xlab("Participant Number") +
  ylab(str_wrap("How strongly do you agree with the statement?", 75)) +
  coord_cartesian(ylim = c(1, 5)) +
  scale_y_continuous(breaks=c(1,2,3,4,5), labels = str_wrap(c("Strongly Disagree","2","3","4","Strongly Agree"),10)) + 
  theme_bw() +
  theme(aspect.ratio = 3/5, strip.text = element_text(size=14),
        axis.text.x = element_text(size=14),
        axis.text.y = element_text(size=14),
        axis.title = element_text(size=20, vjust=0.35),
        panel.grid = element_blank(),
        legend.position = "right",
        legend.text=element_text(size=18),
        legend.title=element_text(size=20))

ggsave("fig2b.jpg",height = 10, width= 15, dpi=600)
```

#3. Force judgments by condition

```{r effect of condition on judgment}

#Run a regression to test the effect of condition on force judgments including a random slope and intercept for subject
mod_rsl <- lme4::lmer(resp_rev ~ rational_irrational + (rational_irrational|sub), data = haddata)
summary(mod_rsl)

mod_rsl_NoCond <- lme4::lmer(resp_rev ~ (rational_irrational|sub), data = haddata)
anova(mod_rsl, mod_rsl_NoCond)
```



```{r descriptives}
#get descriptive statistics
library("mosaic")
favstats(~resp_rev, data = diddata, groups = rational_irrational)
favstats(~resp_rev, data = haddata, groups = rational_irrational)
```
